unit UMenu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvExControls, JvGradient, JvNavigationPane, ToolWin, ComCtrls,
  ExtCtrls, JvXPCore, JvXPBar, JvExComCtrls, JvToolBar, Menus, JvButton,
  ActnList, PlatformDefaultStyleActnCtrls, ActnMan;

type
  TFMenu = class(TForm)
    pnMenu: TPanel;
    JvXPBar1: TJvXPBar;
    JvXPBar2: TJvXPBar;
    JvXPBar3: TJvXPBar;
    JvXPBar4: TJvXPBar;
    MainMenu1: TMainMenu;
    TaskBar: TPanel;
    ActionManager1: TActionManager;
    acCliente: TAction;
    acProduto: TAction;
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure JvXPBar4Items0Click(Sender: TObject);
    procedure acClienteExecute(Sender: TObject);
  private
    { Private declarations }
    procedure AddTaskIcon(Form: TForm; Sender: TObject);
    function FormIsOpen(ClassForm: string): Boolean;
  public
    { Public declarations }
    procedure RemoveTaskIcon(Form: TForm);
  end;

const
  TaskIcoPrefix: String = 'TSK';

var
  FMenu: TFMenu;

implementation

uses UFormCliente;

{$R *.dfm}

function TFMenu.FormIsOpen(ClassForm: string): Boolean;
var
  i: Integer;
begin
  Result := true;
  for i := 0 to MDIChildCount - 1 do
  begin
    if UpperCase(MDIChildren[i].ClassName) = UpperCase(ClassForm) then
    begin
      Result := false;
      Continue;
    end;
  end;
end;

procedure TFMenu.acClienteExecute(Sender: TObject);
begin
  if FormIsOpen('TformCliente') then begin
    formCliente := TformCliente.Create(Self);
    AddTaskIcon(formCliente, Sender);
  end else begin
    formCliente.WindowState := wsNormal;
  end;
  formCliente.Show;
end;

procedure TFMenu.AddTaskIcon(Form: TForm; Sender: TObject);
var
  ico: TJvNavPanelButton;
begin
  if not(Sender is TJvNavPanelButton) then
  begin
    ico := TJvNavPanelButton.Create(TaskBar);
    with ico do
    begin
      Align := alLeft;
      Width := 89;
      Caption := Form.Caption;
      Alignment := taCenter;
      Parent := TaskBar;
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clWindowText;
      Font.Height := -11;
      Font.Name := 'Tahoma';
      Font.Style := [fsBold];
      HotTrackFont.Charset := DEFAULT_CHARSET;
      HotTrackFont.Color := clWindowText;
      HotTrackFont.Height := -11;
      HotTrackFont.Name := 'Tahoma';
      HotTrackFont.Style := [fsBold];
      ParentFont := false;
      Colors.ButtonColorFrom := 15381371;
      Colors.ButtonColorTo := 12927762;
      ImageIndex := 0;
      OnClick := TJvXPBarItem(Sender).OnClick;
      Name := TaskIcoPrefix + Form.Name;
    end;
  end;
end;

procedure TFMenu.FormPaint(Sender: TObject);
var
  altura, coluna: Word;
  cf, X: Integer;
  r, g, b: Integer;
begin
  altura := (ClientHeight + 255) div 256;
  cf := Round(255 / 30);
  r := 169;
  g := 188;
  b := 254;
  X := 0;
  for coluna := 0 to 255 do
  begin
    inc(X);
    if X >= cf then
    begin
      X := 0;
      inc(r);
      inc(g);
      dec(b);
    end;
    with Canvas do
    begin
      Brush.Color := RGB(r, g, b);
      FillRect(Rect(0, coluna * altura, ClientWidth, (coluna + 1) * altura));
    end;
  end;
end;

procedure TFMenu.FormResize(Sender: TObject);
begin
  Invalidate;
end;

procedure TFMenu.JvXPBar4Items0Click(Sender: TObject);
begin
 { if FormIsOpen('TFDefault') then begin
    FDefault := TFDefault.Create(Self);
    AddTaskIcon(FDefault, Sender);
  end else begin
    FDefault.WindowState := wsNormal;
  end;

  FDefault.Show;     }
end;

procedure TFMenu.RemoveTaskIcon(Form: TForm);
var
  ico: TJvNavPanelButton;
  i: Integer;
begin
  for i := 0 to TaskBar.ComponentCount - 1 do
  begin
    if (TaskBar.Components[i] is TJvNavPanelButton) then
    begin
      ico := TJvNavPanelButton(TaskBar.Components[i]);
      if UpperCase(TaskIcoPrefix + Form.Name) = UpperCase(ico.Name) then
      begin
        Break;
      end;
    end;
  end;

  if ico <> nil then
    FreeAndNil(ico);
end;

end.
